package com.leetcode;

import java.util.Scanner;

/**
 * 大数求和
 * 输入两个字符串，计算两个字符串的相加和
 * 问题点：字符串代表的数字可能很大，不能直接利用BigInter来进行相加
 * 解决方法：通过个位数相加进位来解决
 */
public class NC01 {
    public static void main(String[] args) {
        System.out.println("请输入两个字符串，以逗号分隔");
        Scanner scanner = new Scanner(System.in);
        String[] numStrs = scanner.next().split(",");

        String num0 = numStrs[0], num1 = numStrs[1];

        // 从个位数开始相加，一位一位往前递进
        int len0 = num0.length();
        int len1 = num1.length();
        int carry = 0, totalLen = Math.max(len0, len1);

        StringBuilder result = new StringBuilder("");
        for (int i = len0 - 1, j = len1 -1; i >= 0 || j >= 0; i--, j--) {
            int k0 = 0, k1 = 0;
            if (i >= 0) {
                // char转int
                k0 = num0.charAt(i) - '0';
            }
            if (j >= 0) {
                // char转int
                k1 = num1.charAt(j) - '0';
            }
            int ksum = k0 + k1 + carry;
            carry = ksum / 10;
            result.append(ksum % 10 + "");
        }

        if (carry > 0) {
            result.append(carry + "");
        }

        System.out.println(numStrs[0] + "+" + numStrs[1] + " = " + result.reverse().toString());

    }
}
